package com.bdd.service;

import com.bdd.bean.BusLine;
import com.bdd.bean.BusStop;
import com.bdd.bean.Points;
import com.bdd.bean.ResultLine;
import com.bdd.utils.Constant;
import com.bdd.utils.DBConn;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by msi on 2016/4/27 0027.
 */
public class GetLines {
    /**
     * 根据公交站点busstopid
     *
     * @param busstopid
     */
    public double getLineWithin5Km(String busstopid) {
        DBConn dbConn = new DBConn();
        List<ResultLine> results = new ArrayList<>();
        List<BusStop> min_Stops = dbConn.queryArrival_Min(busstopid, Constant.DISTANCE_WALk, Constant.DISTANCE_BUS);
        List<BusStop> max_Stops = dbConn.queryArrival_Max(busstopid, Constant.DISTANCE_WALk, Constant.DISTANCE_BUS);
        for (BusStop min_Stop : min_Stops) {
            for (BusStop max_Stop : max_Stops) {
                //如果是同一条线路则开始计算
                if (min_Stop.getLine().equals(max_Stop.getLine())) {
                    BusLine busLine = dbConn.queryLineByName(min_Stop.getLine());
                    ResultLine result = new ResultLine(dbConn.queryBusStopById(busstopid), min_Stop, max_Stop, busLine);
                    results.add(result);
                }
            }
        }
        return calGrade(results);
    }

    /**
     * 根据x,y来求lineName的5Km内的可达路线
     */
    public double getLineWithin5Km(Points points) {
        DBConn dbConn = new DBConn();
        List<ResultLine> results = new ArrayList<>();
        List<BusStop> min_Stops = dbConn.queryArrival_Min(points, Constant.DISTANCE_WALk, Constant.DISTANCE_BUS);
        List<BusStop> max_Stops = dbConn.queryArrival_Max(points, Constant.DISTANCE_WALk, Constant.DISTANCE_BUS);
        for (BusStop min_Stop : min_Stops) {
            for (BusStop max_Stop : max_Stops) {
                //如果是同一条线路则开始计算
                if (min_Stop.getLine().equals(max_Stop.getLine())) {
                    BusLine busLine = dbConn.queryLineByName(min_Stop.getLine());
                    ResultLine result = new ResultLine(new BusStop(points), min_Stop, max_Stop, busLine);
                    results.add(result);
                }
            }
        }
        return calGrade(results);
    }

    private double calGrade(List<ResultLine> results) {
        double max_1 = 0;
        double max_2 = 0;
        double max_3 = 0;
        double max_4 = 0;
        //查找各个方向上的最大值
        for (int i = 0; i < results.size(); i++) {
            ResultLine resultLine = results.get(i);
            if (resultLine.getGrade().keySet().contains(Constant._1)) {
                double temp_1 = resultLine.getGrade().get(Constant._1);
                if (temp_1 > max_1) {
                    max_1 = temp_1;
                }
            }
            if (resultLine.getGrade().keySet().contains(Constant._2)) {
                double temp_2 = resultLine.getGrade().get(Constant._2);
                if (temp_2 > max_2) {
                    max_2 = temp_2;
                }
            }
            if (resultLine.getGrade().keySet().contains(Constant._3)) {
                double temp_3 = resultLine.getGrade().get(Constant._3);
                if (temp_3 > max_3) {
                    max_3 = temp_3;
                }
            }
            if (resultLine.getGrade().keySet().contains(Constant._4)) {
                double temp_4 = resultLine.getGrade().get(Constant._4);
                if (temp_4 > max_4) {
                    max_4 = temp_4;
                }
            }
        }
        double grade = (max_1 + max_2 + max_3 + max_4) / 4;
        System.out.println("可达性:" + grade);
        return grade;
    }
}
